package cn.easybuy.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 封装连接数据库的方法 关闭连接 增删改通用的方法
 *
 * 查询也可以有个通用的方法
 * 
 * @author Tom
 *
 */
public abstract class BaseDao {

	static String driver = "";// com.mysql.jdbc.Driver";
	static String url = "";// "jdbc:mysql://localhost:3306/petshop?useUnicode=true&characterEncoding=utf-8";
	static String name = "";// "root";
	static String pwd = "";// "123456";

	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	static {

		Properties props = new Properties();
		// 文件名称
		String configFile = "database.properties";
		// 借助文件流读取配置信息
		InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(configFile);

		// load
		try {
			props.load(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("配置信息加载失败");
		}
		// properties 这个类去根据key获取value
		driver = props.getProperty("driver");
		url = props.getProperty("url");
		name = props.getProperty("username");
		pwd = props.getProperty("pwd");
	}

	// 获取连接的方法
	public Connection getConnection() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("驱动加载失败");
		}
		try {
			conn = DriverManager.getConnection(url, name, pwd);
			return conn;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("数据库连接异常");
			return null;
		}
	}

	// 关闭连接
	public void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		try {
			if (null != rs) {
				rs.close();
			}
			if (null != pstmt) {
				pstmt.close();
			}
			if (null != conn) {
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("数据库关闭异常");
		}
	}

	// 增删改通用的方法
	public int exeUpdate(String sql, Object[] params) {
		conn = getConnection();
		try {
			pstmt = conn.prepareStatement(sql); // 執行sql ,(没有给sql的? 参数赋值)

			if (null != params) {// sql里面有? 占位符
				// 循环遍历参数列表,执行赋值
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject((i + 1), params[i]);
				}
			}

			int res = pstmt.executeUpdate();
			return res;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		} finally {
			close(conn, pstmt, rs);
		}
	}

	// 查询 通用的方法
	public ResultSet exeQuery(String sql, Object[] params) {
		conn = getConnection();
		try {
			pstmt = conn.prepareStatement(sql); // 執行sql ,(没有给sql的? 参数赋值)

			if (null != params) {// sql里面有? 占位符
				// 循环遍历参数列表,执行赋值
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject((i + 1), params[i]);
				}
			}

			rs = pstmt.executeQuery();
			return rs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} /*finally { //查询的时候不能关
			close(conn, pstmt, rs);
		}*/
	}
	/**
	 * 需要重写的方法
	 *
	 * @param rs
	 * @return
	 * @throws Exception
	 */
	public abstract Object tableToClass(ResultSet rs) throws Exception;
}
